/*

The MIT License

Copyright (c) 2009 kuneri-api contributors

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

// Read docs for more info:
// http://wiki.kunerilite.net/index.php?title=GPS_plugin

import kuneri.Plugin;
import kuneri.Response;
import kuneri.Configuration;
import kuneri.GPSResponse;

class kuneri.GPS extends Plugin
{
	
	public function GPS(configuration : Configuration)
	{
		name = "gps";
		super(configuration);
	}
	
	public function start(callback : Function) : Void
	{
		userCallback = callback;
		request(COMMAND_START);
	}
	
	public function stop(callback : Function) : Void
	{
		userCallback = callback;
		request(COMMAND_STOP);
	}
	
	public function read(callback : Function) : Void
	{
		userCallback = callback;
		request(COMMAND_READ);
	}
	
	private function requestHandler(data : String, command : String) : Void
	{
		
		super.requestHandler(data, command);
		
		var response : GPSResponse = new GPSResponse();
		response.command = command;
		response.error = Number(responseObject[GPSResponse.KL_ERROR]);
		response.end = Number(responseObject[GPSResponse.KL_END]);
		response.responseObject = responseObject;
		
		if(command == COMMAND_READ && response.error == GPSResponse.SUCCESS)
		{
			
			// Sometimes Kuneri returns commas instead of dots as decimal separators,
			// let's substitute just in case.
			// 
			// Thanks to eagle-eye Elecash for the heads up!
			// http://forum.kuneri.net/index.php/topic,184.0.html
			
			var latitude : String = responseObject["klPosLatitude"];
			latitude = latitude.split(",").join(".");
			
			var longitude : String = responseObject["klPosLongitude"];
			longitude = longitude.split(",").join(".");
			
			if(latitude == undefined || longitude == undefined || latitude == null || latitude == undefined)
			{
				
				
				
			} else {
				
				response.latitude = Number(latitude);
				response.longitude = Number(longitude);
				response.count = Number(responseObject["klCount"]);
				response.speed = Number(responseObject["klSpeed"]);
				
			}
			
		}
		
		userCallback(response);
		
	}
	
	private var userCallback : Function;
	
	public static var COMMAND_START : String = "start";
	
	public static var COMMAND_READ : String = "read";
	
	public static var COMMAND_STOP : String = "stop";
	
}